/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.entityGenerator.base.elements;

import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.baseInfo.EnumConstantInfo;
import com.inspur.edp.caf.generator.enumGen.EnumGenerator;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.collection.GspEnumValueCollection;
import com.inspur.edp.cef.designtime.api.element.EnumIndexType;
import com.inspur.edp.cef.designtime.api.element.GspEnumValue;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.entityGenerator.base.DataTypeGeneratorContext;
import lombok.var;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.NumberLiteral;
import org.eclipse.jdt.core.dom.StringLiteral;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ElementEnumGenerator extends EnumGenerator {
  private IGspCommonField field;
  private DataTypeGeneratorContext dataTypeGenContext;

  public ElementEnumGenerator(IGspCommonField field, DataTypeGeneratorContext dataTypeGenContext) {
    super(dataTypeGenContext.getEntityGenContext().getCompilationUnitInfo());
    this.field = field;
    this.dataTypeGenContext = dataTypeGenContext;
  }

  @Override
  protected void doInitialize() {
    super.doInitialize();
    dataTypeGenContext
        .getEntityGenContext()
        .getElementEnumTypes()
        .put(field.getID(),
                new MediateType(dataTypeGenContext.getEntityGenContext().getDefaultPackageName(), getName()));
  }

  protected IGspCommonField getField() {
    return field;
  }

  @Override
  protected List<EnumConstantInfo> buildEnumCollection() {
    EnumIndexType enumIndexType = this.field.getEnumIndexType();
    List<EnumConstantInfo> result = new ArrayList<EnumConstantInfo>();
    GspEnumValueCollection enumValues = this.field.getContainEnumValues();
    if (enumValues.size() == 0) {
      return result;
    }

    for (int i = 0; i < enumValues.size(); i++) {
      EnumConstantInfo info = new EnumConstantInfo();
      info.setEnumName(((GspEnumValue)enumValues.get(i)).getValue());
      List<Expression> args = new ArrayList<Expression>();

      if (enumIndexType == EnumIndexType.String) {
        StringLiteral strIndex = this.ast.newStringLiteral();
        strIndex.setLiteralValue(((GspEnumValue)enumValues.get(i)).getStringIndex());
        args.add(strIndex);
        info.setArguments(args);
      } else {

        NumberLiteral index = this.ast.newNumberLiteral(String.valueOf(((GspEnumValue)enumValues.get(i)).getIndex()));
        args.add(index);
        info.setArguments(args);
      }
      result.add(info);
    }

    return result;
  }

  @Override
  protected ArrayList<BaseGenerator> createChildGenerators() {
    ArrayList<BaseGenerator> list = super.createChildGenerators();
    if (list == null) {
      list = new ArrayList();
    }

    //TODO 构造函数

    ElementEnumConstructGenerator cons = new ElementEnumConstructGenerator(getName(),field);
    list.add(new ElementEnumFieldGenerator(field,dataTypeGenContext));
    list.add(new ElementEnumInstanceGenerator(getName(),field,dataTypeGenContext));
    list.add(cons);
    list.add(new ElementEnumGetValueGenerator(field));
    list.add(new ElementEnumGetMappingsGenerator(getName(),field));
    list.add(new EnumForValueGenerator(getName(),field));
    if(field.getEnumIndexType()== EnumIndexType.String){
      list.add(new ElementEnumIsStringIndexTypeGenerator(getName(),field));
    }
    return list;
  }

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getAccessModifier() {
    return new ArrayList<Modifier.ModifierKeyword>(
        Arrays.asList(new Modifier.ModifierKeyword[] {Modifier.ModifierKeyword.PUBLIC_KEYWORD}));
  }

  protected static final String nameSuffix = "Enum";

  @Override
  protected String getName() {
    return field.getLabelID() + nameSuffix;
  }

//  @Override
//  protected String getNameSpace() {
//    return dataTypeGenContext.DataType.getGeneratedEntityClassInfo().ClassNamespace;  }

  @Override
  protected void afterGenerate() {
    super.afterGenerate();
    dataTypeGenContext.getEnumTypes().put(field.getLabelID(), getName());
  }

  public static String getName(IGspCommonField field) {
    return field.getLabelID() + nameSuffix;
  }

}
